package ui.controls;


import java.awt.Color;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import ui.Assets;
import ui.components.ImageControl;
import ui.control.dial.DialStates;
import ui.control.dial.DialSizes;
import ui.Console;

public class Dial extends ImageControl implements ui.control.dial.DialControl {

	private DialStates state;
	protected Image image;
	private DialBank owner;
		
	public Dial( Assets assets, DialBank owner, DialSizes size ) {
		this.owner = owner;
		
		switch( size ) {
		case _64:
			imagelist = assets.dial64;
			width = 64;
			height = 64;
			break;
		case _96:
			imagelist = assets.dial96;
			width = 96;
			height = 96;
			break;
		}
		
		setSize( width, height);
		state = DialStates.UP;
		currentImage = 0;
		image = imagelist.image( currentImage );
		//this.setDoubleBuffered( true );
		
		
		
		addMouseListener(
			new MouseAdapter( ) {
				public void mousePressed( MouseEvent e ) {
					mouseDown( e );
					repaint();
				}
				public void mouseReleased( MouseEvent e ) {
					mouseUp( e );
					repaint();
				}
			}
		);
	}

	public void setState( DialStates state ) {
		switch( state ) {
		case UP:
			currentImage = 0;
			image = imagelist.image( currentImage );
			this.state = DialStates.UP;
			repaint();
			break;
		case UPRIGHT:
			currentImage = 1;
			image = imagelist.image( currentImage );
			this.state = DialStates.UPRIGHT;
			repaint();
			break;
		case RIGHT:
			currentImage = 2;
			image = imagelist.image( currentImage );
			this.state = DialStates.RIGHT;
			repaint();
			break;
		case DOWNRIGHT:
			currentImage = 3;
			image = imagelist.image( currentImage );
			this.state = DialStates.DOWNRIGHT;
			repaint();
			break;
		case DOWN:
			currentImage = 4;
			image = imagelist.image( currentImage );
			this.state = DialStates.DOWN;
			repaint();
			break;
		case DOWNLEFT:
			currentImage = 5;
			image = imagelist.image( currentImage );
			this.state = DialStates.DOWNLEFT;
			repaint();
			break;
		case LEFT:
			currentImage = 6;
			image = imagelist.image( currentImage );
			this.state = DialStates.LEFT;
			repaint();
			break;
		case UPLEFT:
			currentImage = 7;
			image = imagelist.image( currentImage );
			this.state = DialStates.UPLEFT;
			repaint();
			break;
		}
	}
	public DialStates getState( ) {
		return state;
	}
	public int positions( ) {
		return 8;
	}
	public void label( String[] labels ) {
		for( int i = 0; i < labels.length; i++ ) {
			
		}
	}
	public DialStates rotateClockwise( ) {
		switch( state ) {
		case UP:
			setState( DialStates.UPRIGHT );
			break;
		case UPRIGHT:
			setState( DialStates.RIGHT );
			break;
		case RIGHT:
			setState( DialStates.DOWNRIGHT );
			break;
		case DOWNRIGHT:
			setState( DialStates.DOWN );
			break;
		case DOWN:
			setState( DialStates.DOWNLEFT );
			break;
		case DOWNLEFT:
			setState( DialStates.LEFT );
			break;
		case LEFT:
			setState( DialStates.UPLEFT );
			break;
		case UPLEFT:
			setState( DialStates.UP );
			break;
		}
		return state;
	}
	public DialStates rotateCounterclockwise( ) {
		switch( state ) {
		case UP:
			setState( DialStates.UPLEFT );
			break;
		case UPRIGHT:
			setState( DialStates.UP );
			break;
		case RIGHT:
			setState( DialStates.UPRIGHT );
			break;
		case DOWNRIGHT:
			setState( DialStates.RIGHT );
			break;
		case DOWN:
			setState( DialStates.DOWNRIGHT );
			break;
		case DOWNLEFT:
			setState( DialStates.DOWN );
			break;
		case LEFT:
			setState( DialStates.DOWNLEFT );
			break;
		case UPLEFT:
			setState( DialStates.LEFT );
			break;
		}
		return state;
	}
	@Override public void mouseDown( MouseEvent e ) {
		switch( e.getButton( ) ) {
		case MouseEvent.BUTTON1:
			rotateClockwise( );
			break;
		case MouseEvent.BUTTON2:
			break;
		case MouseEvent.BUTTON3:
			rotateCounterclockwise( );
			break;
		}
		owner.changed( this, state );
		
		//this.repaint( 0, 0, width, height );
	}
	@Override public void mouseUp( MouseEvent e ) {
		//toggle( );
	}
}
